<template>
    <div>
      <div>
        <el-input
          size="medium"
          class="addCateInput"
          placeholder="添加商品类型"
          suffix-icon="el-icon-circle-plus-outline"
          @keydown.enter.native="handleAdd"
          v-model="category.categoryName">
        </el-input>
        <el-button
          size="medium" icon="el-icon-circle-plus-outline" type="primary"
          @click="handleAdd">添加
        </el-button>
        <el-input
          size="medium"
          class="addCateInput"
          placeholder="输入关键字查询"
          suffix-icon="el-icon-circle-plus-outline"
          @keydown.enter.native="handleSelectKey"
          v-model="category.selectKey">
        </el-input>
        <el-button
          size="medium" icon="el-icon-circle-plus-outline" type="success"
          @click="handleSelectKey">查询
        </el-button>
        <el-button
          size="medium" icon="el-icon-delete" type="danger"
          @click="handleDeleteMany" :disabled="this.multipleSelection.length==0">批量删除
        </el-button>
      </div>
      <div>
        <el-table
          @selection-change="handleSelectionChange"
          height="470"
          ref="singleTable"
          :data="tableData"
          highlight-current-row
          style="width: 100%">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            property="id"
            label="编号"
            width="120">
          </el-table-column>
          <el-table-column
            property="categoryName"
            label="商品类型"
            width="180">
          </el-table-column>
          <el-table-column
            label="创建时间"
            width="220">
            <template slot-scope="scope">
              <i class="el-icon-time"></i>
              <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary" size="mini" icon="el-icon-edit" plain
                @click="showEditView(scope.$index, scope.row)">
              </el-button>
              <el-button
                type="danger" size="mini" icon="el-icon-delete" plain
                @click="handleDelete(scope.$index, scope.row)">
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog title="编辑商品类型"
                 :visible.sync="dialogFormVisible">
        <div>
          <el-tag>商品类型</el-tag>
          <el-input size="medium" v-model="updateCategory.categoryName" class="updateCateInput"></el-input>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="doCancel" size="medium">取 消</el-button>
          <el-button type="primary" @click="doUpdate" size="medium">确 定</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  export default {
    name: 'CategoryManage',
    data(){
      return{
        category:{
          updateTime:'',
          categoryName:'',
          selectKey:''
        },
        updateCategory:{
          categoryName:''
        },
        tableData: [],
        dialogFormVisible:false,
        multipleSelection: []
      }
    },
    mounted(){
      this.initTable();
    },
    methods:{
      //查询全部数据
      initTable(){
        this.getRequest('/system/category/kind/cates').then(resp=>{
          if (resp){
            this.tableData=resp;
          }
        })
      },
      //添加事件
      handleAdd(){
        if (this.category.categoryName){
          this.postRequest('/system/category/kind/',this.category).then(resp=>{
            if (resp){
              this.initTable();
              this.category.categoryName='';
              this.$notify.success({ title: '成功', message: '添加成功' });
            }
          })
        }else {
          this.$notify.info({ title: '消息', message: '商品类型不能为空' });
        }
      },
      //显示弹出框赋值
      showEditView(index,data){
        Object.assign(this.updateCategory,data);
        this.dialogFormVisible=true;
      },
      //修改事件
      doUpdate(){
        this.updateCategory.updateTime=this.getCurrentTime();
        console.log(this.updateCategory.updateTime)
        this.updateCategory.createTime='';
        this.putRequest('/system/category/kind/',this.updateCategory).then(resp=>{
          if (resp){
            this.initTable();
            this.dialogFormVisible=false;
            this.$notify.success({ title: '成功', message: '更新成功' });
          }
        })
      },
      //取消修改事件
      doCancel(){
        this.dialogFormVisible = false;;
        this.$notify.info({ title: '消息', message: '已取消更新' });
      },
      //删除事件
      handleDelete(index,data){
        this.$confirm('此操作将删除"'+data.categoryName+'"商品类型, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/system/category/kind/'+data.id).then(resp=>{
            this.initTable();
          })
          this.$notify.success({ title: '成功', message: '删除成功' });
        }).catch(() => {
          this.$notify.info({ title: '消息', message: '已取消删除' });
        });
      },
      //表格-多选框选中状态
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      //批量删除
      handleDeleteMany(){
        this.$confirm('此操作将删除"'+this.multipleSelection.length+'"个商品类型, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids ='?';
          this.multipleSelection.forEach(item =>{
            ids+='ids='+ item.id + '&';
          })
          this.deleteRequest('/system/category/kind/'+ids).then(resp=>{
            this.initTable();
          })

          this.$notify.success({ title: '成功', message: '删除成功' });
        }).catch(() => {
          this.$notify.info({ title: '消息', message: '已取消删除' });
        });
      },
      //查询
      handleSelectKey(){
          if (this.category.selectKey){
            this.postRequest('/system/category/kind/'+this.category.selectKey).then(resp=> {
              if (resp) {
                this.tableData=resp;
                this.category.selectKey='';
                this.$notify.success({ title: '成功', message: '查询成功' });
              }
            })
          }else {
            this.initTable();
          }
      },
    }
  }
</script>

<style scoped>
  .addCateInput{
    width: 220px;
    margin: 10px 10px 10px 15px;
  }
  .updateCateInput{
    width: 200px;
    margin-left: 10px;
  }

</style>
